import express from 'express';
import { Request, Response } from 'express';
const app = express();
const port = 3000;

/**
 * 使用 JSON 中间件
 */
app.use( express.json() );

app.listen(port , () => {
    console.log('🚀服务已经启动🚀');
}); 

app.get('/', (request:Request, response:Response) => {
    response.send('你好');
});

const data = [
    {
        id:1,
        title:'关山月',
        content:'明月出天山,苍茫云海间'
    },
    {
        id:2,
        title:'关山月',
        content:'明月出天山,苍茫云海间'
    },
    {
        id:3,
        title:'关山月',
        content:'明月出天山,苍茫云海间'
    },
    {
        id:4,
        title:'关山月',
        content:'明月出天山,苍茫云海间'
    },
];


app.get('/posts', (request, resprequest:Request, response:Response) => {
    response.send(data);
 });


app.get('/posts/:postId', (request:Request, response:Response) =>{
    //获取内容 id
    const { postId } = request.params;

    // 查找具体内容
    const posts = data.filter(item => item.id == postId);

    // 做出响应
    response.send(posts[0]);

});


app.post('/posts', (request:Request, response:Response) => {
    // 获取请求里的数据
    const { content } = request.body;

    // 响应状态码
    response.status(201);

    // 输出请求头部数据
    console.log(request.headers['sing-along']);
    
    // 设置响应头部数据
    response.set('Sing-Along','How  I wonder');

    // 做出响应
    response.send({
        message:`成功创建了内容:${content}`
    });
});